<?php
class Slider extends AppModel
{
	var $name = 'Slider';
	
	var $validate = array(
				'title'=>array('rule'=>VALID_NOT_EMPTY,
							   'message'=>'This field can\'t be empty'
								),
				'zone'=>array('rule'=>VALID_NOT_EMPTY,
								   'message'=>'This field can\'t be empty'
								),
				'text'=>array('rule'=>VALID_NOT_EMPTY,
							  'message'=>'This field can\'t be empty'
								),
				'image'=>array('rule'=>VALID_NOT_EMPTY,
							  'message'=>'This field can\'t be empty'
								)
		);	
	const PUBLISHED = 1;
	const UNPUBLISHED = 0;
	
	var $cacheFields = array("sliderInfo"=>"onUpdate");
	function initialize()
	{
		$optionList = Configure::read('Sliders.options');
		parent::initialize();
		 $this->fieldProperties = array(
			'title'=>array(
				'group'=>'add',
				'label'=>'Title'
				),
			'zone'=>array(
					'group'=>'add',
					'label'=>'Zone',
					'type'=>'select',
					'options'=>$optionList
					),
			'text'=>array(
					'group'=>'add',
                                        'type'=>'editor',
					'label'=>'Text'
					),
			'button_text'=>array(
					'group'=>'add',
					'label'=>'Button Text'
					),
			'button_link'=>array(
					'group'=>'add',
					'label'=>'Link'
                                    ),
			'image'=>array(
					'group'=>'add',
                                        'type'=>'file',
					'label'=>'Banner'
					),
			'published'=>array(
						'group'=>'add',
						'type'=>'checkbox'

						)
			);
					//prd($this);
                 
	}
	
	//delete ad for zone that does not exists.
	function deleteAfterZoneEdit()
	{
		$zoneList = $this->_getZoneOptionList();
		return $this->deleteAll(array('not'=>array('Slider.zone'=>$zoneList)));
	}
	
	function _getZoneOptionList()
	{		
		$zoneOptions = array();
		$serializedZoneOptions = cr('Zone.zone');
		if($serializedZoneOptions)
		{				
			$zoneOptions = unserialize($serializedZoneOptions);	
			$zoneOptions = array_map('trim',$zoneOptions);
			$zoneOptions = array_combine($zoneOptions,$zoneOptions);
		}
		return $zoneOptions;	
	}
	
	function getSliderInfo($sliderId)
	{
		$cacheKey = "sliderInfo";
		$options = array("duration"=>"+20 minutes");
		$slider = GigaModelCache::read($this->alias,$sliderId,$cacheKey,$options);
		if($slider === false)
		{
			$slider = $this->findById($sliderId);
			if(!$slider)
			{
				$slider = array();
			}
			GigaModelCache::write($this->alias, $sliderId, $cacheKey, $slider,$options);
		}
		return $slider;
	}
	
	function getZoneAddList($zone)
	{
		$cacheKey = "sliders_zone_list";
		GigaCache::set(array("duration"=>"+20 minutes",'path'=>CACHE));
		$zonesArray = GigaCache::read($cacheKey);
		if($zonesArray === false)
		{
			$slidersList = $this->find("list",array("fields"=>array("id","zone"),'conditions'=>array('published'=>self::PUBLISHED)));
			if(!$slidersList)
			{
				$slidersList = array();
			}
			$zonesArray = array();
			foreach($slidersList as $sliderId => $zoneName)
			{
				$zonesArray[$zoneName][$sliderId] = $sliderId; 
			}
			GigaCache::write($cacheKey,$zonesArray);
		}
		$sliderIds = array();
		if(isset($zonesArray[$zone]))
		{
			$sliderIds = $zonesArray[$zone];
		}
		return $sliderIds;
	}
	
	function afterSave($created)
	{
		GigaCache::set(array("duration"=>"+20 minutes",'path'=>CACHE));
		GigaCache::delete("sliders_zone_list");
		parent::afterSave($created);
	}	
	
	function afterDelete($cs = null)
	{
        GigaCache::set(array("duration"=>"+20 minutes",'path'=>CACHE));
		GigaCache::delete("sliders_zone_list");
		parent::afterDelete($cs);
	}
}
?>